package _8nio;

import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.TimeUnit;

/**
 * @Description:
 * @Author wangzhen
 * @Date 2022-07-11 20:50
 **/
public class BIOTest {


    @Test
    public void client() {

        Socket socket = null;
        OutputStream outputStream = null;
        InputStream inputStream=null;
        try {
            //1.创建客户端对象，并指定连接的IP地址或主机名+端口号
            socket = new Socket("127.0.0.1", 9999);

            //2.获取这个通道的流，输出数据到通道中
            outputStream = socket.getOutputStream();

            //3.发送数据
            outputStream.write("你好".getBytes());
            socket.shutdownOutput();//通过shutdownOutput高速服务器已经发送完数据，后续只能接受数据


            //4.接受响应
            inputStream=socket.getInputStream();
            byte[] bys = new byte[1024];
            int len;
            StringBuilder sb = new StringBuilder();
            while ((len = inputStream.read(bys)) != -1) {
                //注意指定编码格式，发送方和接收方一定要统一，建议使用UTF-8
                sb.append(new String(bys, 0, len,"UTF-8"));
            }
            System.out.println("Client:收到响应:"+sb.toString());

        } catch (Exception e) {
            e.printStackTrace();

        } finally {

            //5.关闭资源
            try {
                if (inputStream != null) {
                    inputStream.close();
                }

                if (outputStream != null) {
                    outputStream.close();
                }

                if (socket != null) {
                    socket.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }


    @Test
    public void server() {

        ServerSocket server=null;
        Socket socket=null;
        InputStream inputStream=null;
        OutputStream outputStream = null;
        try {
            //1.创建Server对象，并绑定端口
            server = new ServerSocket(9999);

            //2.阻塞监听通道，有数据传输就返回一个Socket对象
            socket = server.accept();
            inputStream = socket.getInputStream();
            byte[] bys = new byte[1024];
            int len;
            StringBuilder sb = new StringBuilder();
            while ((len = inputStream.read(bys)) != -1) {//只有当客户端关闭它输出流的时候，服务端才能取得结尾的-1
                sb.append(new String(bys, 0, len, "UTF-8"));
            }

            //3.解析数据
            System.out.println("Server:收到请求:"+sb.toString());
            System.out.println("Server:模拟延迟3s");
            TimeUnit.SECONDS.sleep(3);

            //4.响应客户端
            outputStream = socket.getOutputStream();
            outputStream.write("OK".getBytes());



        } catch (Exception e) {
            e.printStackTrace();

        } finally {

            //5.关闭资源
            try {
                if (outputStream != null) {
                    outputStream.close();
                }

                if (inputStream != null) {
                    inputStream.close();
                }

                if (socket != null) {
                    socket.close();
                }

                if (server != null) {
                    server.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
